package com.emep.changzhi.analyse.service.information;

import com.emep.changzhi.analyse.entity.information.SysMailInfo;
import com.emep.changzhi.analyse.repository.information.SysMailInfoRepository;
import com.emep.changzhi.analyse.utils.MapUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author Wangcc
 * @version V1.0
 * @Description: 通讯录业务层
 * @date 2018/2/26 16:45
 */
@Service
@Slf4j
public class SysMailInfoService {
    
    @Resource
    private SysMailInfoRepository sysMailInfoRepository;


    /**
     *
     * 不含查询条件的分页获取数据
     *
     * @param pageNo
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Page<SysMailInfo> findAll(Integer pageNo, Integer pageSize){
        try {
            Pageable pageable = new PageRequest(pageNo-1, pageSize);
            return  sysMailInfoRepository.findAll(pageable);
        } catch (Exception e) {
            log.error(e.getLocalizedMessage());
            return null;
        }
    }

    /**
     *
     * 新增通讯录
     *
     * @param payload
     * @return
     * @throws Exception
     */
    public SysMailInfo save(Map<String, Object> payload) {
        SysMailInfo SysMailInfo = null;
        try {
            SysMailInfo SysMailInfo1 = (SysMailInfo) MapUtils.convertMap(SysMailInfo.class,payload);
            SysMailInfo = sysMailInfoRepository.save(SysMailInfo1);
        }catch (Exception e){
            log.error(e.getLocalizedMessage());
        }
        return SysMailInfo;
    }


    /**
     *
     * 根据id查询通讯录
     *
     * @param id
     * @return
     */
    public SysMailInfo findId(String id){
        SysMailInfo SysMailInfo = null;
        try {
            SysMailInfo = sysMailInfoRepository.findSysMailInfoById(id);
        }catch (Exception e){
            log.error(e.getLocalizedMessage());
        }
        return SysMailInfo;
    }

    /**
     *
     * 根据id 删除通讯录
     *
     * @param id
     */
    public Boolean delete(String id) {
        Boolean result = false;
        try {
            Integer result1 = sysMailInfoRepository.deleteById(id);
            if(result1 == 1){
                result = true;
            }
        }catch (Exception e){
            log.error(e.getLocalizedMessage());
        }
        return result;
    }

    /**
     *查询所有通讯录
     *
     * @return List<SysMailInfo>
     */
    public List<SysMailInfo> findAll() {
        List<SysMailInfo> SysMailInfos = null;
        try {
            SysMailInfos =  sysMailInfoRepository.findAll();
        }catch (Exception e){
            log.error(e.getLocalizedMessage());
        }
        return SysMailInfos;
    }

}
